<?php

/**
 * 数据调用模块
 * Some rights reserved：abc3210.com
 * Contact email:admin@abc3210.com
 */
class DatacallAction extends AdminbaseAction {

    public $type;

    function _initialize() {
        parent::_initialize();
        //允许调用的数据类型
        $this->type = array(
            1 => "javascript",
            2 => "json",
            3 => "jsonp",
        );
    }

    /**
     * 显示数据调用 
     */
    public function index() {
        $db = M("Datacall");
        $count = $db->count();
        $page = $this->page($count, 20);
        $data = $db->limit($page->firstRow . ',' . $page->listRows)->order(array("id" => "desc"))->select();

        $this->assign("Page", $page->show('Admin'));
        $this->assign("data", $data);
        $this->display();
    }

    /**
     * 添加数据调用 
     */
    public function add() {
        if ($this->isPost()) {
            $db = M("Datacall");
            if (empty($_POST['name'])) {
                $this->error("数据调用名称不能为空！");
            }
            if ((int) $this->_post("type") < 1 || empty($this->type[$this->_post("type")])) {
                $this->error("输出类型不能为空或者选择错误！");
            }
            $_POST['cachetime'] = $_POST['cachetime'] ? (int) $_POST['cachetime'] : 0;
            $_POST = $this->setting($_POST);
            $_POST['setting']['limit'] = $_POST['setting']['limit'] ? (int) $_POST['setting']['limit'] : 10;
            $_POST['setting']['page'] = $_POST['setting']['page'] ? 1 : 0;
            $_POST['setting'] = serialize($_POST['setting']);
            $data = $db->create();
            if ($data) {
                if ($db->add($data)) {
                    $this->success("添加成功！", U("Datacall/Datacall/index"));
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error($db->getError());
            }
        } else {
            import('@.ORG.Util.Form');
            import('@.ORG.Util.Tree');
            $tree = new Tree();
            $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
            $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
            $str = "<option value='\$catid' \$selected \$disabled>\$spacer \$catname</option>";
            $array = F("Category");
            $tree->init($array);
            $categorydata = $tree->get_tree(0, $str, $parentid);

            foreach (F("Model") as $k => $v) {
                $Model[$v['modelid']] = $v['name'];
            }

            $this->assign("category", $categorydata);
            $this->assign("Model", $Model);
            $this->assign("type", $this->type);
            $this->display();
        }
    }

    /**
     * 编辑 
     */
    public function edit() {
        $db = M("Datacall");
        if ($this->isPost()) {
            if (empty($_POST['name'])) {
                $this->error("数据调用名称不能为空！");
            }
            if ((int) $this->_post("type") < 1 || empty($this->type[$this->_post("type")])) {
                $this->error("输出类型不能为空或者选择错误！");
            }
            $_POST['cachetime'] = $_POST['cachetime'] ? (int) $_POST['cachetime'] : 0;
            $_POST = $this->setting($_POST);
            $_POST['setting']['limit'] = $_POST['setting']['limit'] ? (int) $_POST['setting']['limit'] : 10;
            $_POST['setting']['page'] = $_POST['setting']['page'] ? 1 : 0;
            $_POST['setting'] = serialize($_POST['setting']);
            $data = $db->create();
            if ($data) {
                if ($db->save($data)) {
                    $this->success("更新成功！", U("Datacall/Datacall/index"));
                } else {
                    $this->error("更新失败！");
                }
            } else {
                $this->error($db->getError());
            }
        } else {
            $id = (int) $this->_get("id");
            $data = $db->where(array("id" => $id))->find();
            if ($data) {
                $data['setting'] = unserialize($data['setting']);
                
                import('@.ORG.Util.Form');
                import('@.ORG.Util.Tree');
                $tree = new Tree();
                $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
                $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
                $str = "<option value='\$catid' \$selected \$disabled>\$spacer \$catname</option>";
                $array = F("Category");
                foreach($data['setting']['category'] as $k=>$catid){
                    $array[$catid]['selected'] = "selected";
                }
                $tree->init($array);
                $categorydata = $tree->get_tree(0, $str, $parentid);

                foreach (F("Model") as $k => $v) {
                    $Model[$v['modelid']] = $v['name'];
                }

                $this->assign("category", $categorydata);
                $this->assign("Model", $Model);
                $this->assign("configtype", $this->type);
                $this->assign($data);
                $this->display();
            } else {
                $this->error("该数据调用不存在！");
            }
        }
    }
    
    /**
     * 删除 
     */
    public function delete(){
        $id = (int)$this->_get("id");
        $db = M("Datacall");
        $status = $db->where(array("id"=>$id))->delete();
        if($status){
            $this->success("删除成功！");
        }else{
            $this->error("删除失败！");
        }
    }

    /**
     * 配置数据处理 
     */
    protected function setting($_POST) {
        switch ($_POST['setting']['type']) {
            case 'category':
                if (!is_array($_POST['setting']['category'])) {
                    $this->error('请选择栏目！');
                }
                break;
            case 'model':
                if ((int) $_POST['setting']['modeid'] < 1) {
                    $this->error("请选择模型");
                }
                break;
            case 'sql':
                if ($_POST['setting']['sql']) {
                    
                } else {
                    $this->error("SQL语句有误或者不能为空！");
                }
                break;
            default:
                $this->error('调用类型有误！');
                break;
        }
        return $_POST;
    }

}

?>
